//
//  VerifyViewController.m
//  QuickReadComic
//
//  Created by 苗钧豪 on 16/8/27.
//  Copyright © 2016年 苗钧豪. All rights reserved.
//

#import "VerifyViewController.h"
#import "JHMacro.h"
#import "ZWYNetTool.h"
#import "SyntonyViewController.h"
@interface VerifyViewController ()<UITextFieldDelegate>
@property(nonatomic,strong)UIView *textview;// 验证码输入框
@property(nonatomic,strong)UILabel *phoneLabel;// 倒计时
@property(nonatomic,strong)UITextField *phoneTextField;// 输入框
@property(nonatomic,strong)UIButton *button;// 确认按钮
@property(nonatomic,strong)NSTimer *timer;
@end

@implementation VerifyViewController
- (void)viewWillAppear:(BOOL)animated {
    [self.phoneTextField becomeFirstResponder];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 背景颜色
    self.view.backgroundColor = [UIColor colorWithRed:1.000 green:0.827 blue:0.000 alpha:1.000];
    // 界面搭建
    [self setup];
    // Do any additional setup after loading the view.
}
// 监控输入框内容变化，改变button文字颜色
- (void)valueChanged {
    if (_phoneTextField.text.length != 0) {
        [self.button setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    } else {
        [self.button setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
    }
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    [self.textview endEditing:YES];
    [self.view endEditing:YES];
}
// 封装界面搭建
- (void)setup {
    // 输入框和ImageView写在一个自定义View里
    self.textview = [[UIView alloc]initWithFrame:CGRectMake(50, 150, SCREEN_WIDTH - 100, 50)];
    [self.view addSubview:_textview];
    // 输入框界面倒圆角
    CALayer *layer1 = _textview.layer;
    [layer1 setMasksToBounds:YES];
    [layer1 setCornerRadius:10];
    // Label
    self.phoneLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.textview.bounds.size.width - 100, 0, 100, 50)];
    // 属性设置
    _phoneLabel.textAlignment = NSTextAlignmentCenter;
    int i = 60;
    _phoneLabel.text = [NSString stringWithFormat:@"%d",i];
    _phoneLabel.backgroundColor = [UIColor grayColor];
    _phoneLabel.textColor = [UIColor whiteColor];
    [self.textview addSubview:_phoneLabel];
    // 输入框
    self.phoneTextField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, self.textview.bounds.size.width - 100, 50)];
    _phoneTextField.delegate = self;
    // 添加监控
    [_phoneTextField addTarget:self action:@selector(valueChanged)  forControlEvents:UIControlEventAllEditingEvents];
    // 输入框背景颜色
    _phoneTextField.backgroundColor = [UIColor whiteColor];
    // 输入框提示文本
    _phoneTextField.placeholder = @"请输入验证码";
    // 删除按钮
    _phoneTextField.clearButtonMode = UITextFieldViewModeAlways;
    // 键盘样式
    _phoneTextField.keyboardType = UIKeyboardTypePhonePad;
    // Ruturn键变为Done
    _phoneTextField.returnKeyType = UIReturnKeyDone;
    // 添加到自定义视图
    [_textview addSubview:self.phoneTextField];
    // 确认按钮
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    _button.frame = CGRectMake(50, 220, SCREEN_WIDTH - 100, 50);
    [_button setTitle:@"确定" forState:(UIControlStateNormal)];
    // 按钮字体颜色
    [_button setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
    // 按钮背景颜色
    [_button setBackgroundColor:[UIColor colorWithWhite:0.171 alpha:1.000]];
    // button 方法
    [_button addTarget:self action:@selector(sure) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:_button];
    // 确认按钮倒圆角
    CALayer *layer2 = _button.layer;
    [layer2 setMasksToBounds:YES];
    [layer2 setCornerRadius:10];
    // LOGO
    UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(110, 80, SCREEN_WIDTH - 220, 45)];
    imgView.image = [UIImage imageNamed:@"3"];
    [self.view addSubview:imgView];
    // UIlabel & UIButton
    // 用户协议Label
    UILabel *Applabel = [[UILabel alloc]initWithFrame:CGRectMake(50, 280, 120, 30)];
    // 文字
    Applabel.text = @"注册即视为同意";
    // 字体
    Applabel.font = [UIFont fontWithName:@"Arial" size:16];
    // 添加到self.view上
    [self.view addSubview:Applabel];
    // 用户协议Button
    UIButton *Appbutton = [UIButton buttonWithType:UIButtonTypeSystem];
    // 布局
    Appbutton.frame = CGRectMake(150, 280, SCREEN_WIDTH - 190, 30);
    // 文字
    [Appbutton setTitle:@"快看漫画App用户协议" forState:(UIControlStateNormal)];
    // 字体
    Appbutton.titleLabel.font = [UIFont fontWithName:@"Arial" size:16];
    // 方法
    [Appbutton addTarget:self action:@selector(PushDeal) forControlEvents:(UIControlEventTouchUpInside)];
    // 添加到self.view上
    [self.view addSubview:Appbutton];
    // back
    // 返回按钮
    UIButton *backbtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    // 布局
    backbtn.frame = CGRectMake(10, 20, 20, 20);
    // button图片，用背景色假数据占位
    [backbtn setImage:[UIImage imageNamed:@"IMG_0418"] forState:(UIControlStateNormal)];
    // 方法
    [backbtn addTarget:self action:@selector(gohome) forControlEvents:(UIControlEventTouchUpInside)];
    // 添加到self.view
    [self.view addSubview:backbtn];
    [self addTimer];
    
}
- (void)addTimer {
    NSTimer *time = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];
    _timer = time;
    
}
- (void)timeAction {
    int number = [_phoneLabel.text intValue];
    if (number == 0) {
        [_timer invalidate];
        _timer = nil;
        _phoneLabel.text = @"重新发送";
        _phoneLabel.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
        [_phoneLabel addGestureRecognizer:tap];
    } else {
        int newnumber = number - 1;
        _phoneLabel.text = [NSString stringWithFormat:@"%d",newnumber];
    }
    
}
- (void)tap {
    [ZWYNetTool POST:@"http://api.kuaikanmanhua.com/v1/phone/send_code" andBody:[NSString stringWithFormat:@"phone=%@&reason=register",_str] andBodyStyle:ZWYBodyString andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40)];
        label.center = self.view.center;
        label.backgroundColor = [UIColor darkGrayColor];
        label.textColor = [UIColor whiteColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"验证码发送成功";
        [self.view addSubview:label];
        [UIView animateWithDuration:3 animations:^{
            label.alpha = 0;
            int i = 60;
            _phoneLabel.text = [NSString stringWithFormat:@"%d",i];
            [self addTimer];
        }];
    } andFailureBlock:^(NSError *error) {
        
    }];
}
// POP回上一界面
- (void)gohome {
    [self dismissViewControllerAnimated:YES completion:nil];
}
// push 到用户协议界面
- (void)PushDeal {
    
}
#warning 抓取数据
// 确定
- (void)sure {
   
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
